package cn.com.xf.service.impl;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;

import cn.com.xf.common.ConfigUtil;
import cn.com.xf.common.Constants;
import cn.com.xf.dao.TblSiteWeeklyArticleDAO;
import cn.com.xf.dao.TblSiteWeeklyDAO;
import cn.com.xf.dao.TblSiteWeeklyMapDAO;
import cn.com.xf.domain.TblSiteWeekly;
import cn.com.xf.domain.TblSiteWeeklyArticle;
import cn.com.xf.domain.TblSiteWeeklyMap;
import cn.com.xf.service.WeeklyService;

@Service
public class WeeklyServiceImpl implements WeeklyService {

	@Autowired
	private TblSiteWeeklyArticleDAO tblSiteWeeklyArticleDAO;
	@Autowired
	private TblSiteWeeklyMapDAO tblSiteWeeklyMapDAO;
	@Autowired
	private TblSiteWeeklyDAO tblSiteWeeklyDAO;

	@Override
	public List<TblSiteWeekly> selectCatalog() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblSiteWeeklyArticle> selectArticleList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TblSiteWeeklyArticle selectArticle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblSiteWeeklyArticle> selectLast() {
		List<TblSiteWeeklyArticle> list = tblSiteWeeklyArticleDAO.selectLast();
		return list;
	}

	@Override
	public TblSiteWeeklyArticle selectWeeklyArticleById(String articleId) {
		return tblSiteWeeklyArticleDAO.selectWeeklyArticelById(articleId);
	}

	@Override
	public List<TblSiteWeeklyMap> selectMapByWeekId(String weekId) {
		return tblSiteWeeklyMapDAO.selectMapByWeekId(weekId);
	}

	@Override
	public int selectWeeklyCount() {
		return tblSiteWeeklyDAO.selectCount();
	}

	@Override
	public List<TblSiteWeekly> selectWeekly(int begin, int pageSize) {
		return tblSiteWeeklyDAO.selectWeekly(begin, pageSize);
	}

	@Override
	public boolean insertWeekly(HttpServletRequest request) {
		try {
			String[] rect = request.getParameterValues("rect[test][]");
			String[] title = request.getParameterValues("title[test][]");
			String[] content = request.getParameterValues("content[test][]");
			if(title == null)
				return false;
			String weeklyName = request.getParameter("weeklyName");
			String publicMan = request.getParameter("publicMan");
			String publicTimeStr = request.getParameter("publicTime");
			TblSiteWeekly tblSiteWeekly = new TblSiteWeekly();
			tblSiteWeekly.setName(weeklyName);
			tblSiteWeekly.setAdduser(publicMan);
			tblSiteWeekly.setAddTimeStr(publicTimeStr);
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile imgFile = multipartRequest.getFile("weeklyImg");
			if (!imgFile.isEmpty() && !StringUtils.isEmpty(weeklyName)) {

				String newFileName = new SimpleDateFormat("yyyyMMddHHmmssSSS")
						.format(new Date())
						+ "_"
						+ imgFile.getOriginalFilename();

				tblSiteWeekly.setPicture(newFileName);

				String absoluteFilePath = ConfigUtil
						.getValue(Constants.ABSOLUTE_ROOT_NAME)
						+ File.separator + Constants.ABSOLUTE_ROOT_IMAGE_PATH;
				File fileFile = new File(absoluteFilePath);
				if (!fileFile.exists()) {
					fileFile.mkdirs();
				}
				File absoluteFileFile = new File(absoluteFilePath, newFileName);
				imgFile.transferTo(absoluteFileFile);
			} else {
				return false;
			}

			tblSiteWeekly.setAddtime(new Date());
			BigDecimal weeklyID = tblSiteWeeklyDAO.insert(tblSiteWeekly);
			int articleSize = 0;
			int[] index = new int[rect.length];
			BigDecimal[] articleId = new BigDecimal[rect.length];
			for (int i = 0; i < rect.length; i++) {
				if (!StringUtils.isEmpty(title[i])) {
					TblSiteWeeklyArticle article = new TblSiteWeeklyArticle();
					article.setAddTime(new Date());
					article.setTitle(title[i]);
					article.setContent(content[i]);
					article.setWeeklyId(weeklyID);
					index[articleSize] = i;
					articleId[articleSize] = tblSiteWeeklyArticleDAO
							.insert(article);
					articleSize++;
				}
			}

			for (int i = 0; i < articleSize; i++) {
				int temp = (i == articleSize - 1) ? rect.length : index[i + 1];
				for (int j = i; j < temp; j++) {
					TblSiteWeeklyMap map = new TblSiteWeeklyMap();
					map.setCoords(rect[j]);
					map.setMapArticleId(articleId[i]);
					map.setWeeklyId(weeklyID);
					tblSiteWeeklyMapDAO.insert(map);
				}
			}

			return true;

		} catch (Exception e) {
			e.printStackTrace();
			// 因为异常被捕获了，需要手动回滚或者抛出异常
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}

	@Override
	public void deleteWeekly(BigDecimal id) {

		tblSiteWeeklyDAO.deleteByWeeklyID(id);
		tblSiteWeeklyArticleDAO.deleteByWeeklyID(id);
		tblSiteWeeklyMapDAO.deleteByWeeklyID(id);
	}

}
